package com.ruoyi.web.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 翻译算法
 */
@Slf4j
public class TranslateNewUtils {

//    public static void main(String[] args) {
//        String chinaName = translateTitle("Studying North Korea’s academic journals can provide valuable insights into the country’s thoughts about its own trajectory—what ideas are being explored, tested and supported. They may also shed light on debate among scientists and academics regarding the pros and cons of various policies and science and technology (S&T) initiatives. Unfortunately, many of these journals are published only in Korean and can be difficult to obtain outside of the country." +
//                "\nAs part of ongoing research on North Korea’s S&T pursuits, 38 North’s Nonresident Fellow Stephen Mercado has created backgrounders for each of the 41 related journals characterizing what topics they cover and who publishes them. These profiles are based on digital copies of the journals accessed via Tokyo-based DPR Korea Media (Korean Press Media or KPM)."
//        , "http://192.168.3.204:10001/translate");
//        System.out.println("翻译 ： " + chinaName);
//    }

    /**
     * 翻译
     *
     * @param englishTitle  翻译文本
     * @param translateUrl  算法url
     * @return
     */
    public static String translateTitle(String englishTitle, String translateUrl) {
        String englishTitles = englishTitle.replaceAll("<br/>", "\n");
        englishTitles = englishTitles.replaceAll("&nbsp;", "");
        String data = "";
        try {
            String params = "";
            Map<String, String> map = new HashMap<>();
            if (StringUtils.isNotEmpty(englishTitles)) {
                String engTitles[] = englishTitles.split("\n");
                for (String engTitle : engTitles) {

                    /**
                     * ① en ：英文翻译为中文。
                     * ② ge ：德语翻译为中文。
                     * ③ ja：日语翻译为中文。
                     * ④ ko：韩语翻译为中文
                     */
                    String languageType = LanguageCheckerUtils.queryChecker(engTitle);
                    if ("traditionalChinese".equals(languageType)) {
                        String result = TraditionalToSimplifiedConverter.convertToSimple(engTitle);
                        data += StringUtils.isNotEmpty(result) ? "&nbsp;&nbsp;&nbsp;&nbsp;" + result + "<br/>" : "&nbsp;&nbsp;&nbsp;&nbsp;" + engTitle + "<br/>";
                    } else {
                        map.put("q", engTitle);
                        map.put("source", languageType);
                        map.put("target", "zh");
                        map.put("format", "text");
                        map.put("api_key", "");
                        params = JSON.toJSONString(map);
                        log.info("开始调用翻译 params = {}", params);
                        log.info("开始调用翻译 开始时间 = {}", DateUtils.dateToString(new Date()));
                        String translate = HttpClientUtil.sendHttpPostJson(translateUrl, params);
                        log.info("开始调用翻译 结束时间 = {}", DateUtils.dateToString(new Date()));
                        if (StringUtils.isNotEmpty(translate)) {
                            JSONObject jsonObject = JSON.parseObject(translate);
                            if (jsonObject.containsKey("translatedText")) {
                                String resData = jsonObject.getString("translatedText");
                                log.info("调用翻译结果 resData = {}", resData);
                                if (resData.contains("官方网站使用量")) {
                                    continue;
                                }
                                if (resData.contains("使用HTTPS的安全")) {
                                    continue;
                                }
                                if (resData.contains("网站使用")) {
                                    continue;
                                }
                                if (resData.contains("版权所有")) {
                                    continue;
                                }
                                if (resData.contains("浏览器中提供有限的功能")) {
                                    continue;
                                }
                                if (resData.contains("Firefox、Safari")) {
                                    continue;
                                }
                                if (resData.contains("朝中社平壤")) {
                                    continue;
                                }
                                resData = resData.contains("残疾、") ? RemoveDuplicateCharacters.removeDuplicates(resData, "残疾、") : resData;
                                resData = resData.contains("残疾者、") ? RemoveDuplicateCharacters.removeDuplicates(resData, "残疾者、") : resData;
                                resData = resData.contains("FIGG照片来自FIGG,") ? RemoveDuplicateCharacters.removeDuplicates(resData, "FIGG照片来自FIGG,") : resData;
                                resData = resData.contains("船东 ") ? RemoveDuplicateCharacters.removeDuplicates(resData, "船东 ") : resData;
                                resData = resData.contains("目标 ") ? RemoveDuplicateCharacters.removeDuplicates(resData, "目标 ") : resData;
                                resData = resData.contains("喷气式") ? RemoveDuplicateCharacters.removeDuplicates(resData, "喷气式") : resData;
                                resData = resData.contains("气气") ? RemoveDuplicateCharacters.removeDuplicates(resData, "气气") : resData;
                                resData = resData.contains("外交") ? RemoveDuplicateCharacters.removeDuplicates(resData, "外交") : resData;
                                resData = resData.contains("外交事务") ? RemoveDuplicateCharacters.removeDuplicates(resData, "外交事务") : resData;
                                resData = resData.contains("25 美元") ? RemoveDuplicateCharacters.removeDuplicates(resData, "25 美元") : resData;
                                resData = resData.contains("第一章") ? RemoveDuplicateCharacters.removeDuplicates(resData, "第一章") : resData;
                                resData = resData.contains("第二章") ? RemoveDuplicateCharacters.removeDuplicates(resData, "第二章") : resData;
                                resData = resData.contains("第三章") ? RemoveDuplicateCharacters.removeDuplicates(resData, "第三章") : resData;
                                resData = resData.contains("第四章") ? RemoveDuplicateCharacters.removeDuplicates(resData, "第四章") : resData;
                                resData = resData.contains("第五章") ? RemoveDuplicateCharacters.removeDuplicates(resData, "第五章") : resData;
                                resData = resData.contains("第六章") ? RemoveDuplicateCharacters.removeDuplicates(resData, "第六章") : resData;
                                resData = resData.contains("导言") ? RemoveDuplicateCharacters.removeDuplicates(resData, "导言") : resData;
                                resData = resData.contains("结论") ? RemoveDuplicateCharacters.removeDuplicates(resData, "结论") : resData;
                                resData = resData.contains("星期一。") ? RemoveDuplicateCharacters.removeDuplicates(resData, "星期一。") : resData;
                                resData = resData.contains("报告") ? RemoveDuplicateCharacters.removeDuplicates(resData, "报告") : resData;
                                resData = resData.replaceAll("_", "");
                                resData = resData.replaceAll("=", "");
//                            data += resData;
                                data += (StringUtils.isNotEmpty(resData) ? "&nbsp;&nbsp;&nbsp;&nbsp;" : "") + resData + "<br/>";
                            }
                        }
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("调用翻译结果 data = {}", data);
        return data;
    }

    public static String translateBrief(String englishBrief, String translateUrl) {
        String data = translateTitle(englishBrief, translateUrl);
        if (StringUtils.isNotEmpty(data)) {
            data = data.replaceAll("<br/nbsp;", "<br/>&nbsp;");
            data = data.replaceAll("< b/nbsp;", "<br/>&nbsp;");
            data = data.replaceAll("& nbsp;", "&nbsp;");
            data = data.replaceAll("& nb", "&nbsp;");
            data = data.replaceAll("&nsp;", "&nbsp;");
        }
        return data;
    }

    /**
     * 翻译
     *
     * @param englishTitle  翻译文本
     * @param translateUrl  算法url
     * @return
     */
    public static String translateNotBqTitle(String englishTitle, String translateUrl) {
        String data = "";
        try {
            String params = "";
            Map<String, String> map = new HashMap<>();
            if (StringUtils.isNotEmpty(englishTitle)) {
                /**
                 * ① en ：英文翻译为中文。
                 * ② ge ：德语翻译为中文。
                 * ③ ja：日语翻译为中文。
                 * ④ ko：韩语翻译为中文
                 */
                String languageType = LanguageCheckerUtils.queryChecker(englishTitle);
                if ("traditionalChinese".equals(languageType)) {
                    String result = TraditionalToSimplifiedConverter.convertToSimple(englishTitle);
                    data += result;
                } else {
                    map.put("q", englishTitle);
                    map.put("source", languageType);
                    map.put("target", "zh");
                    map.put("format", "text");
                    map.put("api_key", "");
                    params = JSON.toJSONString(map);
                    log.info("开始调用翻译 params = {}", params);
                    log.info("开始调用翻译 开始时间 = {}", DateUtils.dateToString(new Date()));
                    String translate = HttpClientUtil.sendHttpPostJson(translateUrl, params);
                    log.info("开始调用翻译 结束时间 = {}", DateUtils.dateToString(new Date()));
                    if (StringUtils.isNotEmpty(translate)) {
                        JSONObject jsonObject = JSON.parseObject(translate);
                        if (jsonObject.containsKey("translatedText")) {
                            String resData = jsonObject.getString("translatedText");
                            log.info("调用翻译结果 resData = {}", resData);
                            data += resData;
                        }
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("调用翻译结果 data = {}", data);
        return data;
    }

    /**
     * 翻译
     *
     * @param englishTitle  翻译文本
     * @param translateUrl  算法url
     * @return
     */
    public static String translateTypeByTitle(String englishTitle, String translateUrl,
                                              String source, String target) {
        String data = "";
        try {
            String params = "";
            Map<String, String> map = new HashMap<>();
            if (StringUtils.isNotEmpty(englishTitle)) {
                /**
                 * en 英文 ge 德语 ja 日语 ko 韩语 zh 中文
                 */
                map.put("source", source);
                map.put("target", target);
                map.put("format", "text");
                map.put("api_key", "");
                params = JSON.toJSONString(map);
                log.info("开始调用翻译 params = {}", params);
                log.info("开始调用翻译 开始时间 = {}", DateUtils.dateToString(new Date()));
                String translate = HttpClientUtil.sendHttpPostJson(translateUrl, params);
                log.info("开始调用翻译 结束时间 = {}", DateUtils.dateToString(new Date()));
                if (StringUtils.isNotEmpty(translate)) {
                    JSONObject jsonObject = JSON.parseObject(translate);
                    if (jsonObject.containsKey("translatedText")) {
                        String resData = jsonObject.getString("translatedText");
                        log.info("调用翻译结果 resData = {}", resData);
                        data += resData;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("调用翻译结果 data = {}", data);
        return data;
    }

    public static String translateNotBqBrief(String englishBrief, String translateUrl) {
        String data = translateNotBqTitle(englishBrief, translateUrl);
        return data;
    }

}
